package sjtu.ist.warehouse4ws.action;

import java.io.FileNotFoundException;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.xml.sax.SAXException;

import sjtu.ist.warehouse4ws.tool.GenerateWsdl;
import sjtu.ist.warehouse4ws.tool.MyUtils;

import com.opensymphony.xwork2.ActionSupport;

public class GenerateWsdlAction  extends ActionSupport{
	private GenerateWsdl generateWsdl;
	String warnning;
	public void setGenerateWsdl(GenerateWsdl generateWsdl) {
		this.generateWsdl = generateWsdl;
	}

	public String getWarnning() {
		return warnning;
	}
	public void setWarnning(String warnning) {
		this.warnning = warnning;
	}
	
	public String gernerateWsdl() throws Exception {

	
		MyUtils.mkDirectory(ServletActionContext.getServletContext().getRealPath("/upload/files/wadl"));
		String basepath=ServletActionContext.getServletContext().getRealPath("/upload/files/domain/").replaceAll("\\\\", "/")+"/";
		//basepath is dynamic
		String outpath=ServletActionContext.getServletContext().getRealPath("/upload/files/wsdl/").replaceAll("\\\\", "/")+"/";
		String basepath1=ServletActionContext.getServletContext().getRealPath("/upload/files/function/").replaceAll("\\\\", "/")+"/";
		
		HttpServletRequest request = ServletActionContext.getRequest ();
		String xmlName=request.getParameter("nodeName");
		System.out.print(xmlName);
		String path=basepath1+xmlName;
		String outFile=outpath+"Wsdl"+xmlName;
		generateWsdl.setDynamicPath(basepath);

			
					try {
					generateWsdl.writeWsdl(outFile, path);
					warnning="生成WSDL文件成功！";
				} catch (SAXException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					warnning="关联文件缺失，请添加相关文件！";
		
				}catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					warnning="关联文件缺失，请添加相关文件！";
		
				}

		return SUCCESS;

		
		
	}

}
